package com.bifrost.repository;

import com.bifrost.entity.MerchantStore;
import com.bifrost.entity.Promotion;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.Date;
import java.util.List;

/**
 * @author rangf
 * @date 2018/3/5
 */
public interface PromotionRepository extends JpaRepository<Promotion, Long> {

    /**
     * 获取团购、积分商城list
     *
     * @param storeId 门店Id
     * @param type 促销类型
     * @return
     */
    @Query(value = "SELECT * FROM `PROMOTION` P WHERE P.TYPE=?2 AND  P.MERCHANT_ID=?1 AND P.IS_ARCHIVED!=1", nativeQuery = true)
    List<Promotion> getPromotionsList(Long storeId, String type);

    /**
     * 获取秒杀list
     * @param storeId 门店Id
     * @return
     */
    @Query(value = "SELECT * FROM `PROMOTION` P WHERE (P.IS_ARCHIVED=FALSE OR P.IS_ARCHIVED IS NULL) AND P.MERCHANT_ID=?1 AND P.TYPE='SECKILL'", nativeQuery = true)
    List<Promotion> getSeckillListOfMerchantStore(Long storeId);

    /**
     * 获取当天秒杀list
     *
     * @param storeId
     * @return
     */
    @Query(value = "SELECT * FROM `PROMOTION` P WHERE (P.IS_ARCHIVED=FALSE OR P.IS_ARCHIVED IS NULL) AND P.MERCHANT_ID=?1" +
            " AND P.TYPE='SECKILL' AND DATE(P.START_TIME)=CURDATE()", nativeQuery = true)
    List<Promotion> getSeckillListOfToday(Long storeId);

    /**
     * 根据秒杀时间获取List
     * @param store
     * @param startTime
     * @return
     */
    @Query(value = "SELECT * FROM `PROMOTION` P WHERE (P.IS_ARCHIVED=FALSE OR P.IS_ARCHIVED IS NULL) AND P.MERCHANT_ID=?1" +
            " AND P.TYPE='SECKILL' AND P.START_TIME=?2", nativeQuery = true)
    Promotion getSeckillListByStoreAndStartTime(MerchantStore store, Date startTime);
}
